Skip to content

Conversation

nikvoid
Copy link

@nikvoid nikvoid commented Apr 12, 2023

Another attempt to add feature requested in #90.
This PR add html_to! macro that can use existing String as output buffer.
Example:

use maud::{self, html, html_to, Render};

#[test]
fn html_render_to_buffer() {
    let mut buf = String::new();

    html_to! { buf 
        p { "existing" }
    };
    
    assert_eq!(buf, "<p>existing</p>");
}

#[test]
fn html_buffer_reuse() {
    let mut buf = String::new();
    html_to! { buf 
        p { "existing" }
    };
    
    html_to! { buf 
        p { "reused" }
    };
    
    assert_eq!(buf, "<p>existing</p><p>reused</p>");
}

#[test]
fn impl_render_to_html_to() {
    struct Foo;
    impl Render for Foo {
        fn render_to(&self, buffer: &mut String) {

            html_to! { buffer
                a { "foobar" }                
            }
        }
    }

    let rendered = html! {
        p { (Foo) }
    }.into_string();
    
    assert_eq!(rendered, "<p><a>foobar</a></p>");
}

I'm requesting comments on how to improve this and fit it better into existing code

@nikvoid nikvoid changed the title PoC writing to existing buffer with html_to! macro Allow writing to existing html buffer Apr 12, 2023
@nikvoid
Copy link
Author

nikvoid commented Apr 12, 2023

Oh, just realized that this won't work simply when trying to interpolate Render value in html_to! because of &mut reference.
image

… to avoid taking &mut String references (fix Splicing exprs into html_to!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant